//
//  BaseTabBar.m
//  KuaiTu
//
//  Created by 江伟 on 2018/10/24.
//  Copyright © 2018年 WeiJiang. All rights reserved.
//

#import "BaseTabBar.h"
#import "TopImgBtnView.h"
@interface BaseTabBar ()

@property (nonatomic,strong) TopImgBtnView *publiBtnView;

@end

@implementation BaseTabBar
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _publiBtnView= [[TopImgBtnView alloc] initWithFrame:CGRectMake(0, 0, self.width/5.0, 68)];
        [self addSubview:_publiBtnView];
        _publiBtnView.backgroundColor = [UIColor clearColor];
        _publiBtnView.img.image = IMG(@"faBu");
        _publiBtnView.titleLab.text = @"发布";
        _publiBtnView.titleLab.textColor = [UIColor colorWithHexString:@"#8c9095"];
        [_publiBtnView addTapGestureTarget:self action:@selector(tapPubliBtn:)];
    }
    return self;
}

/**
 *  加号按钮点击
 */
- (void)tapPubliBtn:(UIGestureRecognizer *)ges
{
    // 通知代理
    if ([self.myDelegate respondsToSelector:@selector(baseTabBar:didClickPlusButton:)]) {
        [self.myDelegate baseTabBar:self didClickPlusButton:self.plusBtn];
    }
}

/**
 *  想要重新排布系统控件subview的布局，推荐重写layoutSubviews，在调用父类布局后重新排布。
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    _publiBtnView.size = CGSizeMake(self.width/5.0, 64);
    _publiBtnView.top = -20;
    _publiBtnView.centerX = self.centerX;
    _publiBtnView.img.frame = CGRectMake(0, 0, 52, 52);
    _publiBtnView.img.centerX = _publiBtnView.centerX - _publiBtnView.left;
    _publiBtnView.titleLab.frame = CGRectMake(0, _publiBtnView.img.bottom + 5, _publiBtnView.width, 10);
    _publiBtnView.titleLab.font = [UIFont fontWithName:@"PingFangSc-Medium" size:10];
    // 2.设置其他tabbarButton的frame
    CGFloat tabBarButtonW = self.width / 5.0;
    CGFloat tabBarButtonIndex = 0;
    for (UIView *child in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        if ([child isKindOfClass:class]) {
            if (tabBarButtonIndex > 1) {
                // 设置x
                child.left = (tabBarButtonIndex + 1) * tabBarButtonW;
                // 设置宽度
                child.width = tabBarButtonW;
            } else {
                // 设置x
                child.left = tabBarButtonIndex * tabBarButtonW;
                // 设置宽度
                child.width = tabBarButtonW;
            }
            // 增加索引
            tabBarButtonIndex++;
        }
    }
}

@end
